Page({
  data: {
      address_list: null,
      cj:'1',
      activity_id:'0'
  },
  onLoad: function(e) {
      getApp().page.onLoad(this, e);
      if(e.cj){
          this.setData({
              cj: e.cj,
              activity_id:e.activity_id
          }) 
      }
  },
  onShow: function() {
      getApp().page.onShow(this);
      var t = this;
      getApp().core.showNavigationBarLoading(), getApp().request({
          url: getApp().api.user.address_list,
          success: function(e) {
              getApp().core.hideNavigationBarLoading(), 0 == e.code && t.setData({
                  address_list: e.data.list
              });
          }
      });
  },
  pickAddress: function(e) {
      var that = this
      var t = e.currentTarget.dataset.index, a = this.data.address_list[t];
      if(that.data.cj == 2){
          console.log('aa',a)
          wx.showModal({
              title: '领奖',
              content: '是否选择此地址领奖',
              success (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  getApp().request({
                      url: getApp().api.integral_lottery.address_save,
                      method: "POST",
                      data:{
                          address_id: '',
                          name: a.name,
                          mobile: a.mobile,
                          province_id: a.province_id,
                          city_id: a.city_id,
                          district_id: a.district_id,
                          detail: a.detail,
                          activity_id:that.data.activity_id,
                      },
                      success: function (r) {
                        console.log('兑换',r)
                        if (r.code === 0) {
                          wx.showToast({
                              title: '领奖成功',
                              icon: 'success',
                              duration: 2000
                            })
                            setTimeout(function () {  
                              getApp().core.navigateBack();
                             }, 500)
                        }
                      },
                      complete: function (r) {
                        getApp().core.hideLoading();
                      }
                    });
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
      }else{
          getApp().core.setStorageSync(getApp().const.PICKER_ADDRESS, a), getApp().core.navigateBack();
      }
     
  },
  getWechatAddress: function(e) {
      getApp().core.chooseAddress({
          success: function(e) {
              "chooseAddress:ok" == e.errMsg && (getApp().core.showLoading(), getApp().request({
                  url: getApp().api.user.add_wechat_address,
                  method: "post",
                  data: {
                      national_code: e.nationalCode,
                      name: e.userName,
                      mobile: e.telNumber,
                      detail: e.detailInfo,
                      province_name: e.provinceName,
                      city_name: e.cityName,
                      county_name: e.countyName
                  },
                  success: function(e) {
                      1 != e.code ? 0 == e.code && (getApp().core.setStorageSync(getApp().const.PICKER_ADDRESS, e.data), 
                      getApp().core.navigateBack()) : getApp().core.showModal({
                          title: "提示",
                          content: e.msg,
                          showCancel: !1
                      });
                  },
                  complete: function() {
                      getApp().core.hideLoading();
                  }
              }));
          }
      });
  }
});